package fr.vince.java.rps_game;

public class RockPaperScissors {
	
	Result play(RPSEnum p1,RPSEnum p2){
		switch(p1){
		case ROCK:
			switch(p2){
			case ROCK: return Result.TIE;
			case PAPER: return Result.LOST;
			case SCISSORS: return Result.WIN;
			}
		case PAPER:
			switch(p2){
			case ROCK: return Result.WIN;
			case PAPER: return Result.TIE;
			case SCISSORS: return Result.LOST;
			}
		case SCISSORS:
			switch(p2){
			case ROCK: return Result.LOST;
			case PAPER: return Result.WIN;
			case SCISSORS: return Result.TIE;
			}
		}
		return Result.TIE;
	}
	
	Result play(Player p1, Player p2){
		int max = (p1.getNumberOfMoves()>p2.getNumberOfMoves()?p2.getNumberOfMoves():p1.getNumberOfMoves());
		for(int i=0;i<max;i++){
			switch(this.play(p1.getNextMove(), p2.getNextMove())){
			case LOST:
				p2.setScore(p2.getScore()+1);
				break;
			case TIE:
				p1.setScore(p1.getScore()+1);
				p2.setScore(p2.getScore()+1);
				break;
			case WIN:
				p1.setScore(p1.getScore()+1);
				break;
			default:
				break;
			}
		}
		if(p1.getScore()>p2.getScore()){
			System.out.println(p1.getName()+" has won the game");
			return Result.WIN;
		}
		else if(p1.getScore()==p2.getScore()){
			System.out.println(p1.getName()+" and "+p2.getName() + " are tied for the lead");
			return Result.TIE;
		}
		System.out.println(p2.getName()+" has won the game");
		return Result.LOST;
	}
}
